
void read_mode(void){
  pinMode(Data0, INPUT);
  pinMode(Data1, INPUT);
  pinMode(Data2, INPUT);
  pinMode(Data3, INPUT);
  pinMode(Data4, INPUT);
  pinMode(Data5, INPUT);
  pinMode(Data6, INPUT);
  pinMode(Data7, INPUT);
}

void write_mode(void){
  pinMode(Data0, OUTPUT);
  pinMode(Data1, OUTPUT);
  pinMode(Data2, OUTPUT);
  pinMode(Data3, OUTPUT);
  pinMode(Data4, OUTPUT);
  pinMode(Data5, OUTPUT);
  pinMode(Data6, OUTPUT);
  pinMode(Data7, OUTPUT);
}

void addressOutput(void){
  pinMode(A_0, OUTPUT);
  pinMode(A_1, OUTPUT);
  pinMode(A_2, OUTPUT);
  pinMode(A_3, OUTPUT);
  pinMode(A_4, OUTPUT);
  pinMode(A_5, OUTPUT);
  pinMode(A_6, OUTPUT);
  pinMode(A_7, OUTPUT);
  pinMode(A_8, OUTPUT);
  pinMode(A_9, OUTPUT);
  pinMode(A_10, OUTPUT);
  pinMode(A_11, OUTPUT);
  pinMode(A_12, OUTPUT);
  pinMode(A_13, OUTPUT);
  pinMode(A_14, OUTPUT);
  pinMode(A_15, OUTPUT);
  pinMode(A_16, OUTPUT);
  pinMode(A_17, OUTPUT);
  pinMode(A_18, OUTPUT);
}



// 读取数据
uint8_t read_data_bus(void) {
//  return ((digitalRead(Data7) << 7) +
//          (digitalRead(Data6) << 6) +
//          (digitalRead(Data5) << 5) +
//          (digitalRead(Data4) << 4) +
//          (digitalRead(Data3) << 3) +
//          (digitalRead(Data2) << 2) +
//          (digitalRead(Data1) << 1) +
//          digitalRead(Data0));
  return ((GPIOB->IDR) >>8) & 0xff;
}

void write_data_bus(uint16_t dataa){
//  digitalWrite(Data0, data & 1);//也可使用移位 data & (1 << 0)
//  digitalWrite(Data1, (data >> 1) & 1);
//  digitalWrite(Data2, (data >> 2) & 1);
//  digitalWrite(Data3, (data >> 3) & 1);
//  digitalWrite(Data4, (data >> 4) & 1);
//  digitalWrite(Data5, (data >> 5) & 1);
//  digitalWrite(Data6, (data >> 6) & 1);
//  digitalWrite(Data7, (data >> 7) & 1);
  GPIOB->ODR = dataa<<8;
}
void setAddress(uint32_t addr) {
  digitalWrite(A_0, (addr >> 0) & 1);
  digitalWrite(A_1, (addr >> 1) & 1);
  digitalWrite(A_2, (addr >> 2) & 1);
  digitalWrite(A_3, (addr >> 3) & 1);
  digitalWrite(A_4, (addr >> 4) & 1);
  digitalWrite(A_5, (addr >> 5) & 1);
  digitalWrite(A_6, (addr >> 6) & 1);
  digitalWrite(A_7, (addr >> 7) & 1);
//  GPIOB->ODR = addr & 0xff;
//  digitalWrite(A_2, (addr >> 2) & 1);
  

  
  digitalWrite(A_8, (addr >> 8) & 1);
  digitalWrite(A_9, (addr >> 9) & 1);
  digitalWrite(A_10, (addr >> 10) & 1);
  digitalWrite(A_11, (addr >> 11) & 1);
  digitalWrite(A_12, (addr >> 12) & 1);
  digitalWrite(A_13, (addr >> 13) & 1);
  digitalWrite(A_14, (addr >> 14) & 1);
  digitalWrite(A_15, (addr >> 15) & 1);
//  GPIOA->ODR = addr>>8 & 0xff;
  digitalWrite(A_16, (addr >> 16) & 1);
  digitalWrite(A_17, (addr >> 17) & 1);
  digitalWrite(A_18, (addr >> 18) & 1);

}

void chip29(){
  digitalWrite(chipWE, HIGH);
}


void readCommand() {
  for(uint8_t i=0; i< CMD_LEN; i++) cmdBuffer[i] = 0;  // clear command buffer
  char c; uint8_t idx = 0;                        // initialize variables
  do {
    if(Serial.available()) {
      c = Serial.read();
      cmdBuffer[idx++] = c;
    }
  } while (c != '\n' && c != 0x0 && idx < CMD_LEN);
  cmdBuffer[idx - 1] = 0;                     // change last newline to '\0' termination
}

// 返回字芯片字符串
uint32_t hexChip(char* data) {
  uint32_t re = 0;
//  if (data[0] >= 0x30 && data[0] <= 0x39)re += (data[0] - 0x30) * 100000;
  if (data[1] >= 0x30 && data[1] <= 0x39)re += (data[1] - 0x30) * 10000;
  if (data[2] >= 0x30 && data[2] <= 0x39)re += (data[2] - 0x30) * 1000;
  if (data[3] >= 0x30 && data[3] <= 0x39)re += (data[3] - 0x30) * 100;
  if (data[4] >= 0x30 && data[4] <= 0x39)re += (data[4] - 0x30) * 10;
  if (data[5] >= 0x30 && data[5] <= 0x39)re += (data[5] - 0x30) * 1;
  return re;
}

// converts character representing a hex nibble into 4-bit value
uint8_t hexDigit(char c) {
  if      (c >= '0' && c <= '9') return c - '0';
  else if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  else if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  else return 0;
}


// converts string containing a hex byte into 8-bit value
byte hexByte(char* a) {
  return ((hexDigit(a[0])*16) + hexDigit(a[1]));
}


// converts string containing a hex word into 16-bit value
uint16_t hexWord(char* data) {
  return ((hexDigit(data[0])*4096)+
    (hexDigit(data[1])*256)+
    (hexDigit(data[2])*16)+
    (hexDigit(data[3])));
}
